<template>
    <el-card class="box-card">
        <div slot="header" class="clearfix">
            <span>科目管理</span>
        </div>
        <el-table :data="subjects" style="width: 100%" max-height="670" border>
            <el-table-column fixed prop="subjectId" label="科目ID" width="150"></el-table-column>
            <el-table-column fixed prop="subjectName" label="科目名称" width="200"></el-table-column>
            <el-table-column fixed prop="studentCount" label="人数" width="100"></el-table-column>
            <el-table-column fixed prop="weight" label="平时分权重占比" width="100"></el-table-column>
            <el-table-column fixed label="操作" width="150">
                <template slot-scope="scope">
                    <el-button size="mini" type="primary" @click="setWeight(scope.row)">
                        设置权重
                    </el-button>
                </template>
            </el-table-column>
        </el-table>

        <!-- 设置权重的对话框 -->
        <el-dialog title="设置权重" :visible.sync="weightDialogVisible">
            <el-form :model="currentSubject">
                <el-form-item label="科目名称" label-width="100px">
                    <el-input v-model="currentSubject.subjectName" disabled></el-input>
                </el-form-item>
                <el-form-item label="权重" label-width="100px">
                    <el-input-number v-model="currentSubject.weight" :min="0.1" :max="0.9" :step="0.1"
                        label="权重"></el-input-number>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="weightDialogVisible = false">取消</el-button>
                <el-button type="primary" @click="saveWeight">保存</el-button>
            </div>
        </el-dialog>
    </el-card>
</template>

<script>
import { changeWeight, getSubjects } from '@/utils/urlManager';
import axios from 'axios';

export default {
    name: "SubjectManager",
    data() {
        return {
            subjects: [
                {
                    subjectId: 1,
                    subjectName: "数学",
                    studentCount: 30,
                    weight: 0.5
                },
                {
                    subjectId: 2,
                    subjectName: "语文",
                    studentCount: 28,
                    weight: 0.3
                },
                {
                    subjectId: 3,
                    subjectName: "英语",
                    studentCount: 32,
                    weight: 0.4
                }
            ],
            currentSubject: {}, // 当前设置权重的科目
            weightDialogVisible: false // 控制权重对话框的显示与隐藏
        };
    },
    mounted() {
        this.getSubjects();
    },
    methods: {
        getSubjects() {
            axios.get(getSubjects)
                .then((response) => {
                    if (response.data.data != null) {
                        console.log(response.data);
                        this.subjects = response.data.data
                    }
                })
                .catch((err) => {
                    this.$message.error(err)
                    console.error(err);

                })
        },
        setWeight(row) {
            this.currentSubject = { ...row };
            this.weightDialogVisible = true;
        },
        saveWeight() {
            // 保存权重设置
            axios.post(changeWeight, this.currentSubject)
                .then((response) => {
                    if (response.data.data != null) {
                        console.log(response.data);
                        this.$message.success(`权重已更新为：${this.currentSubject.weight}`);
                        this.getSubjects();
                    }
                })
                .catch((err) => {
                    this.$message.error(err);
                    console.error(err);
                })
            this.weightDialogVisible = false;
        }
    }
};
</script>

<style scoped>
.box-card {
    height: 95%;
    width: 90%;
    margin: 20px;
}
</style>